//
//  ZqlVisitorLoginView.swift
//  ZqlSinaWeiBo
//
//  Created by 赵乾龙 on 16/2/16.
//  Copyright © 2016年 Mr.zhao. All rights reserved.
//

import UIKit
import SnapKit

//定义协议
@objc protocol ZqlVisitorLoginViewDelegate: NSObjectProtocol{
    
    //声明可选的代理方法
    optional func userWillLogin()
    
    //注册代理方法
    func userWillRegister()
}


class ZqlVisitorLoginView: UIView {
    
    //声明代理对象
    weak var zqlVLDelegate:ZqlVisitorLoginViewDelegate?
    
    //点击登录的方法
    @objc private func loginBtnDidClick(){
        
        zqlVLDelegate?.userWillLogin?()
    }
    
    //点击注册的方法
    @objc private func registerBtnDidClick(){
        zqlVLDelegate?.userWillRegister()
    }
    
    
    //重写init 自定义访客视图
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置页面信息
    func setUpInfoWith(tipText:String,imageName:String?){
        tipLable.text=tipText
        if imageName != nil{
            circleIcon.image=UIImage(named: imageName!)
            largeHouse.hidden = true
            
            //将circleIcon移动到最顶层
            self.bringSubviewToFront(circleIcon)
            
        }else{
            
            startAnimation()
        }
    }
    
    //圆形图片转动
    private func startAnimation(){
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        anima.repeatCount = MAXFLOAT
        anima.toValue = 2 * M_PI
        anima.duration = 20
        anima.removedOnCompletion = false
        circleIcon.layer.addAnimation(anima, forKey: nil)
    }
    
    
    private func setUpUI(){
        addSubview(circleIcon)
        addSubview(backImage)
        addSubview(largeHouse)
        addSubview(tipLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        //给空间添加约束
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        backImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        tipLable.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon.snp_centerX)
            make.centerY.equalTo(circleIcon.snp_bottom).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLable.snp_left)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLable.snp_right)
            make.top.equalTo(tipLable.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        let value:CGFloat = 236.0/255.0
        self.backgroundColor = UIColor(red: value, green: value, blue: value, alpha: 1.0)
        
        //添加点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    //通过懒加载创建子控件
    //圆环图片
    private lazy var backImage:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var circleIcon:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //大房子图片
    private lazy var largeHouse:UIImageView=UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //文字lable
    private lazy var tipLable:UILabel = UILabel.factoryLabel("关注一些人,回这里看看有什么惊喜", fontSize: 14, textColor: UIColor.lightGrayColor())
       
        
    //登录按钮
    private lazy var loginBtn:UIButton = UIButton(backImageName: "common_button_white_disable", title: "登录", textColor: UIColor.darkGrayColor())
    //注册按钮
    private lazy var registerBtn:UIButton = UIButton(backImageName: "common_button_white_disable", title: "注册", textColor: themeColor)

}





